#coding:UTF-8
import random

TOTAL_BLKS = 4  # 读写"块地址"的取值个数
TOTAL_OPS = 20  # 总操作数
TOTAL_CONTENTS = 4  # 不同内容的个数

blks = [0 for _ in range(TOTAL_BLKS)]

def gen_read() -> str:
    if max(blks) == 0:
        return
    writtens = [i for i in range(TOTAL_BLKS) if blks[i] == 1]
    lbn = random.choice(writtens)

    # read的md5直接随便写
    return f"0 0 cscn {lbn} 1 R 0 0 " + '0' * 32
    
def gen_write() -> str:

    def calc_hash(content, length):
        s = str(content)
        repeat, remain = divmod(length, len(s))
        ret = s * repeat
        return ret + s[:remain]

    lbn = random.randint(0, len(blks)-1)
    blks[lbn] = 1
    content = random.randint(0, TOTAL_CONTENTS-1)
    md5 = calc_hash(content, 32)
    return f"0 0 cscn {lbn} 1 W 0 0 {md5}"


def main():
    gen_funcs = [gen_read, gen_write]
    with open("my.blkparse", 'w') as f:
        f.write("0 0 cscn 11 32 W 0 0 " + '1'*256 + '2'*256 + '3'*256 + '4'*256 + '\n')
        f.write("0 0 cscn 66 8 W 0 0 " + '1'*256 + '\n')
        # for i in range(TOTAL_OPS):
        #     s = gen_funcs[random.randint(0, len(gen_funcs)-1)]()
        #     if s is None:
        #         s = gen_write()
        #     f.write(s + '\n')


if __name__ == '__main__':
    main()